package com.sinog.front.config.xss;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.DispatcherType;
import java.util.Map;

/**
 * @desc xss过滤器
 * @author wangxj
 * @date 2021/11/15 17:37
 */
@Configuration
public class FilterConfig {

    @Value("${gkzx.azbj.xss.enabled}")
    private String enabled;
    @Value("${gkzx.azbj.xss.excludes}")
    private String excludes;
    @Value("${gkzx.azbj.xss.urlPatterns}")
    private String urlPatterns;

    /**
     * 描述
     * @return FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean xssFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        //过滤器类（继承Filter）
        registration.setFilter(new XssFilter());
        registration.addUrlPatterns(StringUtils.split(urlPatterns,","));
        registration.setName("xssFilter");
        registration.setOrder(Integer.MAX_VALUE);
        Map<String,String> initParameters = Maps.newHashMap();
        initParameters.put("excludes",excludes);
        initParameters.put("enabled",enabled);
        //Filter 初始化参数
        registration.setInitParameters(initParameters);
        return registration;
    }
}